package com.mybatisplus.controller;

import com.mybatisplus.entity.BaseResultBean;
import com.mybatisplus.entity.SignData;
import com.mybatisplus.entity.Student;
import com.mybatisplus.service.SignService;
import com.mybatisplus.service.StudentService;
import com.mybatisplus.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Auto created By IntelliJ IDEA
 *
 * @author : gzrong
 * @Date : 2023/5/610:57
 */
@RestController
@RequestMapping("/sign")
public class SignController {
    private boolean isSigning = false;
    @Autowired
    private SignService signService;
    @Autowired
    private StudentService studentService;
    private int type = 0;

    //签到部分开始
    @RequestMapping("/startSign")//开始签到
    public DataVO<String> startSign(HttpServletRequest request) {
        DataVO<String> signStatus = new DataVO<>();
        this.type = Integer.parseInt(request.getParameter("type"));
        if (!isSigning) {
            signService.clean();//清空签到表，然后开始签到
            isSigning = true;
            signStatus.setCode(1);
            signStatus.setMsg("签到发起成功!");
        } else {
            signStatus.setCode(-1);
            signStatus.setMsg("签到发起失败:签到正在进行!");
        }
        return signStatus;
    }

    @RequestMapping("/stopSign")//停止签到
    public DataVO<String> stopSign(HttpServletRequest request) {
        DataVO<String> signStatus = new DataVO<>();
        if (isSigning) {
            isSigning = false;
            System.out.println("签到已停止");
            this.type = 0;
            List<SignData> signedStudent = signService.getTableData();//拿到已签到学生信息
            //利用foreach将所有已签到的学生进行加分操作
            for (SignData s : signedStudent
            ) {
                String stuid = s.getStuid();//拿出学生学号
                Integer score = studentService.selectStudentByStuid(stuid).getScore();//查询学生分数
                studentService.updateScoreByStuid(score + 1, stuid);//学生分数+1并写入数据库
            }
            signStatus.setMsg("签到已停止!");
            signStatus.setCode(1);
        } else {
            signStatus.setMsg("签到停止失败:签到开始!");
            signStatus.setCode(-1);
        }
        return signStatus;
    }

    //获取签到状态
    @RequestMapping("/signStatus")
    public boolean signStatus() {
        return this.isSigning;
    }

    //学生签到请求地址
    @RequestMapping("/sign")
    public BaseResultBean sign(HttpServletRequest request, HttpServletResponse response) {
        BaseResultBean bean = new BaseResultBean();
        String stuid;
        String name;
        int type = Integer.parseInt(request.getParameter("type"));
        if (type == this.type) {
            if (type == 1) {
                Student student = (Student) request.getSession().getAttribute("student");
                stuid = student.getStuid();
                name = student.getName();
            } else {
                stuid = request.getParameter("stuid");
                name = request.getParameter("name");
            }
            if (isSigning) {
                if (!signService.isAlreadySigned(stuid)) {//查询是否已签到
                    if (signService.intoSign(name, stuid)) {//查询是否
                        bean.setStatus(isSigning);
                        bean.setMessage("签到成功!签到人名字:" + name);
                    } else {
                        bean.setMessage("签到失败!我们未找到你的信息!\n名字:" + name + "\n学号：" + stuid);
                    }
                } else {
                    bean.setMessage(name + ":你已成功签到，请不要重复签到");
                    bean.setStatus(true);
                }
            } else {
                bean.setStatus(false);
                bean.setMessage("签到失败，签到未开始或者已过签到时间");
            }
        } else {
            bean.setStatus(false);
            bean.setMessage("签到类型错误，请向老师询问签到类型");
        }

        return bean;
    }

    @RequestMapping("/getSign")//获取已签到学生信息
    public DataVO<SignData> getSign() {
        DataVO<SignData> data = new DataVO<>();
        if (isSigning) {
            data.setData(signService.selectAllInSign().getData());
            data.setCode(1);
            data.setMsg("获取成功！");
        } else {
            data.setCode(-1);
            data.setMsg("签到未开始！");
        }
        return data;
    }

    @RequestMapping("/getNotSigned")//获取未签到学生信息
    public DataVO<SignData> getNotSigned() {
        DataVO<SignData> data = new DataVO<>();
        if (isSigning) {
            data.setData(signService.selectNotSigned().getData());
            data.setCode(1);
            data.setMsg("获取成功！");
        } else {
            data.setCode(-1);
            data.setMsg("签到未开始！");
        }
        return data;
    }

    @RequestMapping("/getSignType")
    public int getType() {
        return this.type;
    }
}
